function world()
    if p == nil then
        return game
    else
        return p:getWorld()
    end
end

removing = false

local script = [[
function shakeCamera()
    i = 1
    while i > 0 do
        local vec = Vec2D(math.random(1, 3), math.random(1, 3)):mul(Vec2D(i, i))
        local rotation = math.random(-4, 4) * i
        if math.random() > 0.5 then
            vec = vec:mul(Vec2D(-1, -1))
        end
        game.CurrentCamera.Offset = vec
        game.CurrentCamera.RotationOffset = rotation
        i = i - 0.25
        sleep(0.02)
    end
    game.CurrentCamera.Offset = Vec2D(0, 0)
    game.CurrentCamera.RotationOffset = 0
end
link(script.Parent.Collided, function(part, velocity) if math.abs(velocity.y) > 20 and part.Collidable and part.CharacterCollide and part.Layer == script.Parent.Layer then shakeCamera() end end)
link(script.Parent.TerrainCollided, function() if script.Parent.Velocity.y < -20 then shakeCamera() end end)

]]

p = nil
isDead = false
function giveTools()
    tools = world():getService("StarterInventory"):getChildren()
    for i=1, #tools do
        tools[i]:clone().Parent = p.Inventory
    end
end
function loadCharacter()
    if game:getType() == 0 then
        c = create("Character")
        c.Skin = "http://2dworlds.org/game/getskin.php?u=$NAME"

        t = create("Box")
        t.Name = "Body"
        t.Locked = true
        t.Size = Vec2D(2.5, 10.5)
        t.Position = world():getService("SpawnManager"):getSpawn()
        t.Bounciness = 0
        t.Transparency = 1
        link(t.OutOfBounds, function() c.Health = 0 end)
        link(t.Removed, function() if c.Parent ~= nil and not removing then c.Health = 0 end end)
        l = create("Label")
        l.Text = "$NAME"
        l.Parent = t
        t.Parent = c

        f = create("FixedRotation")
        f.Parent = t

        ct = create("VelocityController")
        ct.Name = "Controller"
        ct.Parent = t

        c.Name = "$NAME"
        c.Parent = world():getService("World")

        s = create("Script")
        s.Name = "CameraScript"
        s.Source = script
        s.Parent = t
        return c
    else
        if game.Storage:getChild("DefaultCharacter") == nil then
            print("This game needs a character. Add a box or a circle, add a texture if you'd like,")
            print("then right click it in the navigator and click Set Default Character.")
        else
            c = game.Storage.DefaultCharacter:clone()
            c.Body:setLinearDamping(2)
            c.Body:setAngularDamping(2)
            c.Name = "$NAME"
            c.Parent = world():getService("World")
            return c
        end
    end
end
p = create("Player")
link(p.Removed, function() if c ~= nil then c:remove() end end)
p.Name = "$NAME"
link(p.Died, function()
        p.Died:disable()
        if isDead then
            return
        end
        isDead = true
        world():asyncTask(function()
            sleep(3)
            world():scheduleTask(function()
                k = p.Inventory:getChildren()
                for i=1, #k do
                    k[i]:remove()
                end
                c.Died:disable()
                removing = true
                c:remove()
                p.Character = loadCharacter()
                world():getService("Players"):setLocalPlayer(p);
                p.Died:enable()
                isDead = false
                giveTools()
                removing = true
            end)
        end)
end)
world():scheduleTask(function()
    inv = create("Inventory")
    inv.Parent = p
    cam = create("Camera")
    cam.Parent = p
    game.CurrentCamera = cam
    p.Character = loadCharacter()
    world():getService("Players"):setLocalPlayer(p)
    local ui = create("PlayerUI")
    ui.Name = "UI"
    ui.Parent = p
    local s = create("PlayerStats")
    s.Name = "Stats"
    s.Parent = p
    p.Parent = world():getService("Players")
    giveTools()
end)